//
//  ApiService.swift
//  Kosspot
//
//  Created by 胡辉 on 2025/6/21.
//

import Moya
enum ApiService {
    case captchaImage
    case login(username: String, password: String, code: String, uuid: String)
    case updatePwd(newPassword: String,oldPassword:String)
    case updateUserInfo(nickname: String, gender: Int, birthday: String, height: String?, weight: String?)
    case userProfile
    case fitnessList
    case profileAvatar (avatarUrl: String)
    case sendSMCcode(mobile: String, scene: String)
    case loginByCode(mobile: String, code: String)
    case dynamicAdd(userId: Int, content: String, address:String, mediaUrls:[String], createTime:Int)
    case dynamicList
    case dynamicLike (dynamicId:Int)
    case dynamicUnlike (dynamicId:Int)
    case logout
    case oauthLogin(oauthType:String,openid:String,accessToken:String,unionid:String)
    case bindMobile(accessToken:String,oauthType:String,openid:String,unionid:String,mobile:String,code:String)

    
    
    
    
}

extension ApiService: TargetType {
    var baseURL: URL { URL(string: "http://120.24.168.207:9090/")! }
    
    var path: String {
        switch self {
        case .captchaImage:
            return "captchaImage"
        case .login:
            return "login"
        case .sendSMCcode:
            return "api/user/send-sms-code"
        case .updatePwd:
            return "system/user/profile/updatePwd"
        case .userProfile:
            return "system/user/profile"
        case .fitnessList:
            return "api/fitness-info/list"
        case .profileAvatar:
            return "api/user/update-avatar"
        case .updateUserInfo:
            return "api/user/update-info"
        case .loginByCode:
            return "api/user/login-by-code"
        case .dynamicAdd:
            return "api/dynamic/add"
        case .dynamicList:
            return "api/dynamic/list"
        case .dynamicLike:
            return "api/dynamic/like"
        case .dynamicUnlike:
            return "api/dynamic/unlike"
        case .logout:
            return "api/user/logout"
        case .oauthLogin:
            return "api/user/oauth-login"
        case .bindMobile:
            return "api/user/bind-mobile"
        }
    }
    var method: Moya.Method {
        switch self {
        case .captchaImage:
            return .get
        case .login:
            return .post
        case .updatePwd:
            return .put
        case .userProfile:
            return .get
        case .fitnessList:
            return .get
        case .profileAvatar:
            return .post
        case .updateUserInfo:
            return .post
        case .sendSMCcode:
            return .post
        case .loginByCode:
            return .post
        case .dynamicAdd:
            return .post
        case .dynamicList:
            return .get
        case .dynamicLike:
            return .post
        case .dynamicUnlike,.logout,.oauthLogin,.bindMobile:
            return .post
        
        }
    }
    
    var task: Task {
        switch self {
        case .captchaImage,.logout:
            return .requestPlain
        case .login(let username, let password, let code, let uuid):
            return .requestParameters(
                parameters: [
                    "username": username,
                    "password": password,
                    "code": code,
                    "uuid": uuid
                ],
                encoding: JSONEncoding.default
            )
        case .updatePwd(let newPassword, let oldPassword):
            return .requestParameters(
                parameters: ["newPassword" : newPassword,
                             "oldPassword" : oldPassword
                            ],
                encoding: JSONEncoding.default)
            
        case .userProfile:
            return .requestPlain
            
        case .fitnessList:
            return .requestPlain
            
        case let .updateUserInfo(nickname, gender, birthday, height, weight):
            var parameters: [String: Any] = [
                "nickname": nickname,
                "gender": gender,
                "birthday": birthday
            ]
            
            height.flatMap { parameters["height"] = $0 }
            weight.flatMap { parameters["weight"] = $0 }
            
            return .requestParameters(parameters: parameters, encoding: URLEncoding.queryString)
        case .sendSMCcode(mobile: let mobile, scene: let scene):
            return .requestParameters(
                parameters: [
                    "scene": scene,
                    "mobile": mobile
                ],
                encoding: URLEncoding.queryString
            )
        case .loginByCode(mobile: let mobile, code: let code):
            return .requestParameters(
                parameters: [
                    "code": code,
                    "mobile": mobile
                ],
                encoding: URLEncoding.queryString
            )
        case .profileAvatar(avatarUrl: let avatarUrl):
            return .requestParameters(
                parameters: [
                    "avatarUrl": avatarUrl
                ],
                encoding: URLEncoding.queryString
            )
        case .dynamicAdd(userId: let userId, content: let content, address: let address, mediaUrls: let mediaUrls, createTime: let createTime):
            return .requestParameters(
                parameters: ["userId" : userId,
                             "content" : content,
                             "address" : address,
                             "mediaUrls" : mediaUrls,
                             "createTime" : createTime
                            ],
                encoding: JSONEncoding.default)
        case .dynamicList:
            return .requestPlain
        case .dynamicLike(dynamicId: let dynamicId):
            return .requestParameters(
                parameters: [
                    "dynamicId": dynamicId,
                ],
                encoding: URLEncoding.queryString
            )
        case .dynamicUnlike(dynamicId: let dynamicId):
            return .requestParameters(
                parameters: [
                    "dynamicId": dynamicId,
                ],
                encoding: URLEncoding.queryString
            )
        case .oauthLogin(oauthType: let oauthType, openid: let openid, accessToken: let accessToken, unionid: let unionid):
            return .requestParameters(
                parameters: [
                    "oauthType": oauthType,
                    "openid": openid,
                    "accessToken": accessToken,
                    "unionid": unionid
                ],
                encoding: URLEncoding.queryString
            )
        case .bindMobile(accessToken:let accessToken,oauthType: let oauthType, openid: let openid, unionid: let unionid, mobile: let mobile, code: let code):
            return .requestParameters(
                parameters: [
                    "accessToken": accessToken,
                    "oauthType": oauthType,
                    "openid": openid,
                    "unionid": unionid,
                    "mobile": mobile,
                    "code": code
                ],
                encoding: URLEncoding.queryString
            )
            
            
        }
    }
    
    var headers: [String: String]? {
        switch self {
        case .captchaImage, .login,.sendSMCcode,.loginByCode,.oauthLogin,.bindMobile:
            return ["Content-Type": "application/json"]

        case .userProfile, .updatePwd, .fitnessList,.profileAvatar,.dynamicAdd,.dynamicList,.dynamicLike,.dynamicUnlike,.logout, .updateUserInfo:

            return ["Content-Type": "application/json",
                    "App-Token": KeychainHelper.getToken() ?? ""]
        
        }
    }
}
